package com.hengjun.baccarat.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hengjun.baccarat.pojo.CellObject2;
import com.hengjun.baccarat.pojo.ViewDataObject;

public class GetJSON {
	public static void setLoginJson(String strResult) {
		try {
			JSONObject jsonObject = new JSONObject(strResult)
					.getJSONObject("BacCartSysuer");
			BcSysUser bcSysUser = BcSysUser.getbcSysUser();
			bcSysUser.setUserIndex(jsonObject.getInt("userIndex"));
			bcSysUser.setUid(jsonObject.getString("uid"));
			bcSysUser.setName(jsonObject.getString("name"));
			bcSysUser.setPower(jsonObject.getString("power"));
			bcSysUser.setFindex(jsonObject.getString("findex"));

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<RecordCore> setListJson(String srtResult) {
		List<RecordCore> rcList = new ArrayList<RecordCore>();
		try {
			JSONObject jsonObject = new JSONObject(srtResult)
					.getJSONObject("list");
			jsonObject.getString("totalRecord");
			jsonObject.getString("totalPage");
			JSONArray array = jsonObject.getJSONArray("recordList");

			for (int i = 0; i < array.length(); i++) {
				RecordCore rc = new RecordCore();
				JSONObject ob = (JSONObject) array.get(i);
				rc.setRecordsn(ob.getString("recordsn"));
				rc.setRecordindex(ob.getInt("recordindex"));
				rc.setRecorddate(ob.getString("recorddate"));
				rc.setReocordmonth(ob.getInt("recordmonth"));
				rc.setGambleaddr(ob.getString("gambleaddr"));
				rc.setVipparty(ob.getString("vipparty"));
				rc.setAddMoney(ob.getDouble("addMoney"));
				rc.setWinorlose(ob.getDouble("winorlose"));
				rc.setRealyCaptial(ob.getDouble("realyCaptial"));
				if (ob.getString("bacCartSysuerIndex") != null
						&& !ob.getString("bacCartSysuerIndex").equals("")
						&& !ob.getString("bacCartSysuerIndex").equals("null")) {
					rc.setBacCartSysuserIndex(ob.getInt("bacCartSysuerIndex"));
				}

				rc.setUserName(ob.getString("userName"));
				rc.setCapital(ob.getDouble("capital"));
				rcList.add(rc);
			}

			return rcList;
		} catch (JSONException e) {
			e.printStackTrace();
			return rcList;
		}

	}

	public static void getRowList(String result) throws Exception {
		try {
			JSONObject ob = new JSONObject(result);
			JSONArray jsonObject = ob.getJSONArray("list");
			ViewDataObject viewDataObject = ViewDataObject.getViewDataObject();
			for (int i = 0; i < jsonObject.length(); i++) {
				JSONObject tempArray = (JSONObject) jsonObject.get(i);
				if (i != 0) {

					List<Object> objectsList = getList(tempArray
							.getJSONArray("list"));
					// 1:大路 2:大眼 3:小眼 4:小強
					if (i == 1) {
						viewDataObject.setBigStreetArrayList(objectsList);
					}
					if (i == 2) {
						viewDataObject.setBigEyeList(objectsList);
					}
					if (i == 3) {
						viewDataObject.setLittleEyeList(objectsList);
					}
					if (i == 4) {
						viewDataObject.setXiaoQiangList(objectsList);
					}

				} else {
					List<String> list = getBettingResultStr(tempArray
							.getJSONArray("list"));
					viewDataObject.setBettingResultList(list);
				}

			}
			if (jsonObject.length() == 0) {
				ViewDataObject.resetViewData();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static List<String> getBettingResultStr(JSONArray array)
			throws Exception {
		List<String> bettingResultList = new ArrayList<String>();
		for (int i = 0; i < array.length(); i++) {
			JSONObject ob = array.getJSONObject(i);
			bettingResultList.add(ob.toString());
		}
		return bettingResultList;
	}

	private static List<Object> getList(JSONArray array) throws JSONException {
		List<Object> mainlist = new ArrayList<Object>();
		for (int i = 0; i < array.length(); i++) {

			JSONArray jsonOArray = (JSONArray) array.get(i);
			List<CellObject2> list = new ArrayList<CellObject2>();
			for (int j = 0; j < jsonOArray.length(); j++) {

				JSONObject ob = jsonOArray.getJSONObject(j);
				CellObject2 cellObject2 = new CellObject2();
				String content = ob.getString("b");
				String[] str = content.split(",");
				cellObject2.setOpenType(str[0]);
				cellObject2.setType(str[1]);
				cellObject2.setTieContinus(Integer.parseInt(str[2]));
				cellObject2.setBeforeType(str[3]);
				cellObject2.setRow(Integer.parseInt(str[4]));
				cellObject2.setCol(Integer.parseInt(str[5]));
				cellObject2.setRealityRow(Integer.parseInt(str[6]));
				cellObject2.setRealityCol(Integer.parseInt(str[7]));
				cellObject2.setWinTotal(Double.parseDouble(str[8]));
				list.add(cellObject2);

			}
			mainlist.add(list);
		}
		return mainlist;
	}

	public static String getListJson(List<Object> list) {

		if (list.size() == 0) {

			return "";
		}

		StringBuilder string = new StringBuilder("{\"list\":[");
		for (int i = 0; i < list.size(); i++) {
			string.append("[");
			@SuppressWarnings("unchecked")
			List<CellObject2> cellList = (List<CellObject2>) list.get(i);
			for (int j = 0; j < cellList.size(); j++) {
				CellObject2 cellObject2 = cellList.get(j);
				string.append("{\"b\":").append("\"")
						.append(cellObject2.getOpenType() + ",")
						.append(cellObject2.getType() + ",")
						.append(cellObject2.getTieContinus() + ",")
						.append(cellObject2.getBeforeType() + ",")
						.append(cellObject2.getRow() + ",")
						.append(cellObject2.getCol() + ",")
						.append(cellObject2.getRealityRow() + ",")
						.append(cellObject2.getRealityCol() + ",")
						.append(cellObject2.getWinTotal() + "\"");

				if (j == cellList.size() - 1) {
					string.append("}");
				} else {
					string.append("},");
				}
			}
			if (i == list.size() - 1) {
				string.append("]");
			} else {
				string.append("],");
			}
		}
		string.append("]}");
		return string.toString();
	}
}
